<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template slot="menu" slot-scope="scope">
        <el-button type="text"
                   size="small"
                   v-if="(scope.row.status===2) && (userData.roleName==='user')"
                   icon="el-icon-circle-plus-outline"
                   @click="dialog(scope)">支付
        </el-button>
        <el-button type="text"
                   size="small"
                   v-if="(scope.row.status===3) && (userData.roleName==='store')"
                   icon="el-icon-truck"
                   @click="deliver(scope)">发货
        </el-button>
        <el-button type="text"
                   size="small"
                   icon="el-icon-delete"
                   v-if="(scope.row.status===2) && (userData.roleName==='user')"
                   @click="rowDel(scope.row)">删除
        </el-button>

        <el-button type="text"
                   size="small"
                   v-if="(scope.row.status===3) && (userData.roleName==='user')"
                   disabled>等待发货
        </el-button>

        <el-button type="text"
                   size="small"
                   v-if="(scope.row.status===5) && (userData.roleName==='user')"
                   icon="el-icon-ship"
                   @click="receiving(scope)">收货
        </el-button>

        <el-button type="text"
                   size="small"
                   v-if="(scope.row.status===6) && (userData.roleName==='user')"
                   icon="el-icon-position"
                   @click="evaluate(scope)">评价
        </el-button>

      </template>
      <template slot-scope="{row}"
                slot="storeName">
        <el-tag>{{row.storeName}}</el-tag>
      </template>
      <template slot-scope="{row}"
                slot="commodityName">
        <el-tag>{{row.commodityName}}</el-tag>
      </template>
      <template slot-scope="{row}"
                slot="commodityPrice">
        <el-tag>{{row.commodityPrice}}</el-tag>
      </template>
      <template slot-scope="{row}"
                slot="specs">
        <el-tag>{{row.specs}}</el-tag>
      </template>
    </avue-crud>
    <el-dialog
      title="提示"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="30%"
      :before-close="handleClose">
      <span>未开发支付功能，仅供测试使用。</span>
      <span slot="footer" class="dialog-footer">
    <el-button type="primary" @click="buy">支付测试</el-button>
  </span>
    </el-dialog>
    <el-dialog
      title="提示"
      :visible.sync="evaluateVisible"
      :close-on-click-modal="false"
      :before-close="handleClose">
      <avue-form v-model="evaluateForm" :option="evaluateOption" @submit="evaluateSubmit"></avue-form>
    </el-dialog>
  </basic-container>
</template>

<script>
  import {getOrderList, getDetail, add, update, remove} from "@/api/cart/cart";
  import {mapGetters} from "vuex";
  import { fil_status } from "@/util/filterstaticdata";
  import { sendOneWebSocket } from "@/api/websocket";
  import { getUser} from "@/api/user";
  import {add as evaluateAdd} from "@/api/evaluate/evaluate";
  export default {
    data() {
      return {
        evaluateVisible:false,
        dialogVisible:false,
        evaluateForm:{},
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          delBtn:false,
          column: [
            {
              label: "商家名称",
              prop: "storeName",
              slot: true,
              rules: [{
                required: true,
                message: "请输入商家名称",
                trigger: "blur"
              }]
            },
            {
              label: "商品名称",
              prop: "commodityName",
              slot: true,
              search: true,
              rules: [{
                required: true,
                message: "请输入商品名称",
                trigger: "blur"
              }]
            },
            {
              label: "商品价格",
              prop: "commodityPrice",
              slot: true,
              rules: [{
                required: true,
                message: "请输入商品价格",
                trigger: "blur"
              }]
            },
            {
              label: "商品图片",
              prop: "commodityImg",
              type:"img",
              dataType: "string",
              rules: [{
                required: true,
                message: "请输入商品图片",
                trigger: "blur"
              }]
            },
            {
              label: "商品规格",
              prop: "specs",
              slot: true,
              rules: [{
                required: true,
                message: "请输入商品规格",
                trigger: "blur"
              }]
            },
            {
              label: "订单状态",
              prop: "status",
              formatter: function (row, value) {
                return fil_status(value);
              },
            },
            {
              label: "时间",
              prop: "updateTime",
            },
          ]
        },
        evaluateOption: {
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          column: [
            {
              label: "评价图片",
              prop: "img",
              type: 'upload',
              span: 24,
              dataType: "string",
              propsHttp: {
                res: 'data'
              },
              listType: 'picture-card',
              action: '/api/pxx_commodity/commodity/upload',
            },
            {
              label: "评分",
              prop: "score",
              type: 'rate',
              showText: true,
              texts: ['极差', '失望', '一般', '满意', '非常满意'],
              rules: [{
                required: true,
                message: "请输入评分",
                trigger: "blur"
              }]
            },
            {
              label: "评价内容",
              prop: "content",
              type:"textarea",
              maxlength:"30",
              span: 24,
              rules: [{
                required: true,
                message: "请输入评价内容",
                trigger: "blur"
              }]
            },
          ]
        },
        data: [],
        buyData:'',
        storeLoginId:'',
        userData:''
      };
    },
    computed: {
      ...mapGetters(["userInfo","permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.cart_add, false),
          viewBtn: this.vaildData(this.permission.cart_view, false),
          delBtn: this.vaildData(this.permission.cart_delete, false),
          editBtn: this.vaildData(this.permission.cart_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },

    mounted() {
      this.getUserData()
    },
    methods: {
      evaluateSubmit(scope,done,loading){
        const row = this.evaluateForm;
        evaluateAdd(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
        const cartRow = {
          id:this.evaluateForm.cartId,
          status: 7
        };
        update(cartRow).then(() => {
          this.onLoad(this.page);
        }, error => {
          window.console.log(error);
        });
        this.evaluateVisible=false
      },
      evaluate(scope){
        this.evaluateVisible=true
        console.log(scope.row)
        this.evaluateForm ={
          storeId:scope.row.storeId,
          cartId:scope.row.id,
          commodityId:scope.row.commodityId
        }
      },

      receiving(scope){
        const row = {
          id:scope.row.id,
          status: 6
        };
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "收货成功!"
          });
        }, error => {
          window.console.log(error);
        });
      },
      deliver(scope){
        const row = {
          id:scope.row.id,
          status: 5
        };
        const messageDTO = {
          receiveId:scope.row.createUser,
          type:'订单推送',
          msg: '您购买的商品【'+scope.row.commodityName+'】,尺码【'+scope.row.specs+'】已经发货！'
        };
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          sendOneWebSocket(messageDTO).then(() => {
          });
        }, error => {
          window.console.log(error);
        });
      },
      getUserData(){
        getUser().then(res => {
          this.userData = res.data.data
        });
      },
      dialog(scope){
        this.dialogVisible = true;
        this.buyData ={
          id:scope.row.id,
          stoerId:scope.row.storeId,
          status:3,
        }
      },
      buy(){
        var row = this.buyData;
        const messageDTO = {
          receiveId:this.buyData.stoerId,
          type:'订单推送',
          msg: '您有一个新的订单!'
        };
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "支付成功!"
          });
          sendOneWebSocket(messageDTO).then(() => {
          });
        }, error => {
          window.console.log(error);
        });
        this.dialogVisible = false;
      },
      rowSave(row, done, loading) {
        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        console.log(row)
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getOrderList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
